package com.example.administrator.dagger2demo.ui.welcome;

import android.util.Log;

import com.example.administrator.dagger2demo.http.RestApi;
import com.example.administrator.dagger2demo.domain.ReleaseInfo;

import java.util.ArrayList;

import javax.inject.Inject;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/4/13.
 */
public class WelcomePresenter implements WelcomeContract.WelcomeActionsListener {

    private final RestApi _restApi;
    private final WelcomeContract.View mview;

    @Inject
    public WelcomePresenter(RestApi restApi, WelcomeContract.View view) {
        Log.i("TAG", "进入了...WorkPresenter");
        this._restApi = restApi;
        Log.i("TAG",_restApi==null?"进入了...is null":"进入了...not null");
        this.mview = view;
        Log.i("TAG",mview==null?"进入了...is null":"进入了...not null");
    }

    @Override
    public void login(String u, String p) {
        Log.i("TAG",_restApi==null?"_restApi==null":"_restApi!=null");
        loginSuccess("message");
    }

    @Override
    public void loginSuccess(String message) {
        Observable<ArrayList<ReleaseInfo>> observable =  _restApi.requestIndex();
        observable
                .subscribeOn(Schedulers.io())//订阅调度在io流线程
                .observeOn(AndroidSchedulers.mainThread())//观察调度主线程
                .subscribe(new Observer<ArrayList<ReleaseInfo>>() {//订阅
                    @Override
                    public void onCompleted() {
                        Log.i("TAG","onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("TAG","onError="+e.toString());
//                       Retrofit  _retrofit =  DemoApplication.getAppComponent().getrRetrofit();
                        Log.i("TAG",_restApi==null?"_retrofit==null":"_retrofit!=null");
//                        ApiException exception = ApiException.create(e, _retrofit);
                    }

                    @Override
                    public void onNext(ArrayList<ReleaseInfo> releaseInfos) {
                        Log.i("TAG","onNext="+releaseInfos.size());
                    }
                });

    }

    @Override
    public void loginError(String message) {

    }

}
